package net.kingborn.core.util;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;

import net.kingborn.core.log.Logger;

/**
 * 日志工具
 * 
 * @author David Chen
 *
 */
public class LogKit {

	public static StringBuilder beginLogInfo(String info) {
		StringBuilder sb = new StringBuilder();
		sb.append(info + "\n");
		return sb;
	}

	public static StringBuilder appendLogInfo(StringBuilder sb, String nextInfo) {
		sb.append(nextInfo + "\n");
		return sb;
	}

	/**
	 * 记录异常
	 * 
	 * @param logger
	 * @param ex
	 */
	public static void logException(Logger logger, Exception ex) {
		logException(logger, ex, null, null, null);
	}

	/**
	 * 记录异常
	 * 
	 * @param logger
	 * @param ex
	 * @param info
	 *            额外信息
	 */
	public static void logException(Logger logger, Exception ex, String info) {
		logException(logger, ex, null, null, info);
	}

	/**
	 * 记录异常
	 * 
	 * @param logger
	 * @param ex
	 * @param reqArgsMap
	 */
	public static void logException(Logger logger, Exception ex, Map<String, Object> reqArgsMap) {
		logException(logger, ex, null, reqArgsMap, null);
	}

	/**
	 * 记录异常
	 *  
	 * @param logger
	 * @param ex
	 * @param request
	 */
	public static void logException(Logger logger, Exception ex, HttpServletRequest request) {
		logException(logger, ex, request, null, null);
	}

	/**
	 * 记录异常
	 * 
	 * @param logger
	 * @param ex
	 * @param request
	 * @param info
	 *            额外信息
	 */
	public static void logException(Logger logger, Exception ex, HttpServletRequest request,
			Map<String, Object> reqArgsMap, String info) {
		PrintWriter pw = null;
		try {
			StringWriter sw = new StringWriter();
			pw = new PrintWriter(sw);
			ex.printStackTrace(pw);

			StringBuilder sb = new StringBuilder();
			sb.append("exception: \n" + sw.toString() + "\n");
			sb.append(wrapRequestArgs(request, reqArgsMap));

			// 额外信息
			if (info != null) {
				sb.append("info: \n" + info + "\n");
			}
			sb.append("---------------------------------------------------------------------------------------\n");

			logger.error(sb.toString());

		} catch (Exception e) {
		} finally {
			try {
				if (pw != null) {
					pw.close();
				}
			} catch (Exception e2) {
			}
		}
	}
	
	/**
	 * info级别信息控制台输出
	 * 
	 * @param logger
	 * @param ex
	 * @param reqArgsMap
	 */
	public static void logInfo(Logger logger,String info) {
		try {
			logger.info(info);
			
		}catch(Exception e){
			e.printStackTrace();
			
		}
		
	}

	// 获得请求参数
	@SuppressWarnings("rawtypes")
	private static StringBuilder wrapRequestArgs(HttpServletRequest request, Map<String, Object> reqArgsMap) {
		StringBuilder sb = new StringBuilder();
		sb.append("client request context: \n");
		if (request != null) {
			sb.append("ip: " + request.getRemoteAddr() + " port: " + request.getRemotePort() + "\n");
			sb.append("locale: " + request.getLocale().toString() + "\n");

			sb.append("request heade args: \n");
			@SuppressWarnings("unchecked")
			Enumeration<String> headNames = request.getHeaderNames();
			while (headNames.hasMoreElements()) {
				String hName = headNames.nextElement();
				sb.append("    key: " + hName + " value: " + request.getHeader(hName) + "\n");
			}

			sb.append("request url args: \n");
			Map reqMap = request.getParameterMap();
			for (Object entry : reqMap.entrySet()) {
				sb.append("    key: " + ((Entry) entry).getKey() + " value: "
						+ Arrays.toString((String[]) (((Entry) entry).getValue())) + "\n");
			}

		}

		// 处理过的
		if (reqArgsMap != null) {
			sb.append("request client args: \n");
			for (Entry<String, Object> entry : reqArgsMap.entrySet()) {
				sb.append("    key: " + entry.getKey() + " value: " + entry.getValue() + "\n");
			}
		}

		return sb;
	}
}
